package com.cw.jeeyt.dao.impl.facade;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.cw.jeeyt.common.utils.StringUtil;
import com.cw.jeeyt.dao.intf.facade.FacadeParamDao;
import com.cw.jeeyt.dao.mapper.facade.FacadeParamMapper;
import com.cw.jeeyt.dao.model.facade.FacadeParamDo;
import com.cw.jeeyt.dao.model.facade.FacadeParamExample;
import com.cw.jeeyt.dao.model.facade.FacadeParamKeyDo;
import com.cw.jeeyt.dao.query.facade.FacadeParamQuery;
import com.cw.lang.common.dao.AbstractBaseDaoImpl;
import com.cw.lang.common.pagehelper.PageHelper;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 接口参数数据库操作接口实现
 *
 * @author chenw
 * @date 2019-10-31 22:35:52
 */
@Repository
public class FacadeParamDaoImpl extends AbstractBaseDaoImpl<FacadeParamDo, FacadeParamMapper>
    implements FacadeParamDao {
    /**
     * 分页查询
     *
     * @param facadeParam 条件
     * @param pageParam 分页参数
     * @return
     */
    @Override
    public PageInfo<FacadeParamDo> selectPage(FacadeParamQuery facadeParam, PageParam pageParam) {
        FacadeParamExample example = buildExample(facadeParam);
        return PageHelper.<FacadeParamDo>startPage(pageParam.getPageNum(), pageParam.getPageSize()).doSelectPageInfo(
            () -> getMapper().selectByExample(example));
    }

    /**
     * 根据key查询接口参数
     *
     * @param id
     * @return
     */
    @Override
    public FacadeParamDo selectByKey(Long id) {
        FacadeParamKeyDo facadeParamKeyDo = new FacadeParamKeyDo();
        facadeParamKeyDo.setId(id);
        return getMapper().selectByPrimaryKey(facadeParamKeyDo);
    }

    /**
     * 根据key删除接口参数
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByKey(Long id) {
        FacadeParamKeyDo facadeParamKeyDo = new FacadeParamKeyDo();
        facadeParamKeyDo.setId(id);
        return getMapper().deleteByPrimaryKey(facadeParamKeyDo);
    }

    /**
     * 查询接口参数信息
     *
     * @param facadeParam 条件
     * @return List<FacadeParamDo>
     */
    @Override
    public List<FacadeParamDo> selectByExample(FacadeParamQuery facadeParam) {
        return getMapper().selectByExample(buildExample(facadeParam));
    }

    /**
     * 新增接口参数信息
     *
     * @param facadeParam 条件
     * @return int>
     */
    @Override
    public int insertBySelective(FacadeParamDo facadeParam) {
        if (facadeParam == null) {
            return -1;
        }
        return getMapper().insertSelective(facadeParam);
    }

    /**
     * 修改接口参数信息
     *
     * @param facadeParam
     * @return
     */
    @Override
    public int updateBySelective(FacadeParamDo facadeParam) {
        if (facadeParam == null) {
            return -1;
        }
        return getMapper().updateByPrimaryKeySelective(facadeParam);
    }

    /**
     * 构建接口参数Example信息
     *
     * @param facadeParam
     * @return
     */
    public FacadeParamExample buildExample(FacadeParamQuery facadeParam) {
        FacadeParamExample example = new FacadeParamExample();
        FacadeParamExample.Criteria criteria = example.createCriteria();
        if (facadeParam != null) {
            //添加查询条件
            if (null != facadeParam.getId()) {
                criteria.andIdEqualTo(facadeParam.getId());
            }
            if (StringUtil.isNotEmpty(facadeParam.getParamCode())) {
                criteria.andParamCodeEqualTo(facadeParam.getParamCode());
            }
            if (StringUtil.isNotEmpty(facadeParam.getParamName())) {
                criteria.andParamNameEqualTo(facadeParam.getParamName());
            }
            if (StringUtil.isNotEmpty(facadeParam.getParamType())) {
                criteria.andParamTypeEqualTo(facadeParam.getParamType());
            }
            if (null != facadeParam.getPublicParam()) {
                criteria.andPublicParamEqualTo(facadeParam.getPublicParam());
            }
            if (StringUtil.isNotEmpty(facadeParam.getParamComment())) {
                criteria.andParamCommentEqualTo(facadeParam.getParamComment());
            }
            if (StringUtil.isNotEmpty(facadeParam.getChildParams())) {
                criteria.andChildParamsEqualTo(facadeParam.getChildParams());
            }
            if (null != facadeParam.getCreateBy()) {
                criteria.andCreateByEqualTo(facadeParam.getCreateBy());
            }
            if (null != facadeParam.getCreateDateBegin()) {
                criteria.andCreateDateGreaterThanOrEqualTo(facadeParam.getCreateDateBegin());
            }
            if (null != facadeParam.getCreateDateEnd()) {
                criteria.andCreateDateLessThanOrEqualTo(facadeParam.getCreateDateEnd());
            }
            if (null != facadeParam.getUpdateBy()) {
                criteria.andUpdateByEqualTo(facadeParam.getUpdateBy());
            }
            if (null != facadeParam.getUpdateDateBegin()) {
                criteria.andUpdateDateGreaterThanOrEqualTo(facadeParam.getUpdateDateBegin());
            }
            if (null != facadeParam.getUpdateDateEnd()) {
                criteria.andUpdateDateLessThanOrEqualTo(facadeParam.getUpdateDateEnd());
            }
        }
        return example;
    }
}
